<?php
namespace app\admin\controller;
use think\Db;
use think\Session;
use think\Controller;
use think\Request;
class Login  extends Controller
{
    public function index()
    {

		$this->view->assign('Assets', Assets);



        return $this->view->fetch();
    }





    public function checklogin(Request $request)
    {
    	if($request->isPost()){



        $code=input('captcha');
       
        $captcha = new \think\captcha\Captcha();  
        $result=$captcha->check($code);  
        if($result==false){
            return '10'; 
        } 




    		$pusername=trim(input('username'));
    		$ppassword=input('password');

            /*
            $cSalt  = 'HBTD*&^GIS&#@*&^';
            $mpassword=MD5(MD5(trim($ppassword)).$cSalt);

            */
            $mpassword=md5($ppassword);

            //return $mpassword;

    		if($pusername !='' && $ppassword !=''){


    		    //跳过登陆
/*
                Session::set('pusername',$pusername);
                Session::set('password',$mpassword);
                $this->success('登陆成功!跳转中...',url('index/index'));

*/

    			$where['master']=$pusername;
    			$check=Db('admin')->where($where)->find();

    			if(!$check){
    				//$this->error('用户不存在!');
    				return '0';
    			}else{
    				if(trim($check['pword'])==trim($mpassword)){


                        $dat = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$_SERVER['REMOTE_ADDR']);
                        $info=json_decode($dat,$assoc=true);
                        $info=$info['data'];
                        $ares=$info['country']."_".$info['region']."_".$info['city']."_".$info['isp'];

                        $data['user']=$pusername;
                        $data['time']=date('Y-m-d H:i:s',time());
                        $data['content']='login_'.$pusername."_".$_SERVER['REMOTE_ADDR']."_".$ares;
                        Db('log') -> insert($data);

                        $admin['logintime']=date('Y-m-d H:i:s',time());
                        $where['master']=$pusername;
                        Db('admin')->where($where)->update($admin);




    					Session::set('cmuser',$pusername);
    					Session::set('cmpw',$mpassword);
    					//$this->success('登陆成功!跳转中...',url('index/index'));
    					return '1';
    				}else{
    					//$this->error('密码错误！');
                        return '-1';
    				}
    			}



    		}else{
    			//$this->error('请填写完整!');
                return '-2';
    		}
    	}else{
    		//$this->error('提交方式错误！请重新提交。');
            return '99';
    	}
    }


    //显示验证码  
    public function show_captcha(){  
        ob_clean();
        $captcha = new \think\captcha\Captcha();  
        $captcha->imageW=121;  
        $captcha->imageH = 32;  //图片高  
        $captcha->fontSize =14;  //字体大小  
        $captcha->length   = 4;  //字符数  
        $captcha->fontttf = '5.ttf';  //字体  
        $captcha->expire = 30;  //有效期  
        $captcha->useNoise = false;  //不添加杂点  
        return $captcha->entry();  
    }

    public function loginout(Request $request)
    {

            Session::clear('think');
            //return '1';
            $this->success('退出成功!', url('login/index'));

    }

}
